package com.baselibrary.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import com.baselibrary.utils.NetworkUtil;

/**
 * Created by dingmouren on 2018/8/1.
 * 监听网络状态变更的广播接收器
 */

public class NetworkChangedReceiver extends BroadcastReceiver {

    private static final String TAG = "NetworkChangedReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction() == ConnectivityManager.CONNECTIVITY_ACTION) {
            /*判断当前网络时候可用以及网络类型*/
            int netWorkType = NetworkUtil.getNetWorkType(context);
            boolean isConnected = NetworkUtil.isAvailable(context);
            if (mOnNetWorkChangedCallBack != null)
                mOnNetWorkChangedCallBack.onNetWorkChanged(isConnected, netWorkType);
        }
    }

    private OnNetWorkChangedCallBack mOnNetWorkChangedCallBack;

    public void setOnNetWorkChangedCallBack(OnNetWorkChangedCallBack changedCallBack) {
        mOnNetWorkChangedCallBack = changedCallBack;

    }

    public interface OnNetWorkChangedCallBack {
        void onNetWorkChanged(boolean isNetWork, int NetType);
    }
}